package cn.hcbycloud.auth.service;


import cn.hcbycloud.auth.bo.CustInfoBO;
import cn.hcbycloud.auth.mapper.CustExtendMapper;
import cn.hcbycloud.auth.mapper.CustMapper;
import cn.hcbycloud.auth.model.Cust;
import cn.hcbycloud.auth.model.CustExtend;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户基本信息表
 *
 * @author gaoheng
 * @date 2022-01-20 15:44:42
 */
@Service
@AllArgsConstructor
public class CustService extends ServiceImpl<CustMapper, Cust> {
    private CustMapper custMapper;
    private CustExtendMapper custExtendMapper;

    /**
     * 判断客户信息是否存在
     *
     * @param mainId
     * @return
     */
    public boolean haveCustomer(String mainId,String tenantId) {
        return custMapper.haveCustomer(mainId,tenantId);
    }

    /**
     * 添加客户基本信息
     * @param custInfoBO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String addCustInfo(CustInfoBO custInfoBO) {
        Cust cust = custInfoBO.getCust();
        cust.preInsert();
        custMapper.insert(cust);
        CustExtend custExtend = custInfoBO.getCustExtend();
        custExtend.setCustId(cust.getId());
        custExtend.preInsert();
        custExtendMapper.insert(custExtend);
        return cust.getId();
    }
}
